<?php

class keywords_class{

private $path='keywords'; //当前模块目录
private $dbver=100; //当前模块的数据库版本

public function __construct(){
	global $king;

	$dbver=(int)$king->getModuleVer($this->path);
	if($this->dbver > $dbver){
		if($this->install_update($dbver)){//成功则返回True，则更新数据库中的版本信息
			$array=array(
				'ndbver'=>$this->dbver,
			);
			$king->db->update('%s_module',$array,"kpath='{$this->path}'");
		}
	}
}

public function tag($kid=''){
	global $king;

	return '';
}


/* ------>>> 安装部分 <<<---------------------------- */

public function install(){
	global $king;


	//_keywords
	$sql='ktitle char(100) not null,
	kname int(11) not null,
	ktag char(100) not null,
	kcontent text null,	
	nshow tinyint(1) not null default 0,
	norder int not null default 0,
	ndate int(10) not null default 0,
	INDEX(ktitle)';

	$king->db->createTable('%s_key',$sql,'kid');

	$king->db->query('ALTER TABLE %s_tag ADD (
	nshow tinyint(1) not null default 0,
	ndate int(10) not null default 0);');

/*

	$sql2='ktitle char(100) not null,
	ndate int(10) not null default 0,
	nshow int(10) not null default 0,
	INDEX(ktitle)';
	
	$king->db->createTable('%s_key_tag',$sql2,'kid');
*/
	$sql3='keyid int not null default 0,
	tagid int not null default 0,
	INDEX(keyid)';
	
	$king->db->createTable('%s_key_tag_id',$sql3,'kid');

	$this->install_update($this->dbver);



	//写模块安装记录
	if(!$king->db->getRows_one('SELECT * FROM %s_module where kpath=\''.$this->path.'\';')){
		$_array=array(
			'kname' =>$king->lang->get($this->path.'/name'),
			'kpath' =>$this->path,
			'kdb'   =>'',
			'ndate' =>time(),
			'ndbver'=>$this->dbver,
			'norder'=>$king->db->neworder('%s_module'),
			'nshow'=>0,
			);
		$kid1=$king->db->insert("%s_module",$_array);

		return true;
	}else{
		return false;
	}
}

public function install_update($ver){


	return True;
}





public function k_ins_key($arrr){
		global $king;

		
		$arrr['ktitle']=str_replace(" ","",$arrr['ktitle']); 	
		
		
		


		$cachepath="gx/keywords";
		if(!$blockto=$king->cache->get($cachepath,time()-'3600')){		
						$array=$king->db->getRows("select ktag from %s_tag where nshow='1'");
						foreach($array as $val){
								$blockto.=$val['ktag'].'|';
							}
						$king->cache->put($cachepath,$blockto.'敏感词');
						$blockto=$blockto.'敏感词';
			}		
		
			if(preg_match("/$blockto/i",$arrr['ktitle'])){    
					$da='1';//包含敏感词
		   }else{    
					$da='0';//不包含敏感词
		   }   
		
		
		
		if($da=='0' && keywords_class::length($arrr['ktitle'])>5){				
				if(!$data=$king->db->getRows_one("select ktitle from %s_key where ktitle='".$arrr['ktitle']."'")){
					$kid=$king->db->insert('%s_key',$arrr);
					return $kid;
				}
		}
	
	}

public function k_ins_tag($array,$kid){
		global $king;	
		$_array='';
		foreach($array as $val){
			$_array=array(
				'ktag'=>$val,
				'ndate'=>time(),
			);
		
			if(!$data=$king->db->getRows_one("select kid from %s_tag where ktag='".$val."'")){
				$id=$king->db->insert('%s_tag',$_array);
				$_array=array(
					'keyid'=>$kid,
					'tagid'=>$id
				);
				
			}else{
				$_array=array(
					'keyid'=>$kid,
					'tagid'=>$data['kid'],
				);
			}


			$king->db->insert('%s_key_tag_id',$_array);
		
		}
		
	}



public function k_ins_fc_tag($array='',$id='0'){
		global $king;	

	
		foreach($array as $key => $val){
			$_array[$key]=array(
				'ktag'=>$array[$key],
				'ndate'=>time(),
				'nshow'=>$id,
			);
			if(!$data=$king->db->getRows_one("select kid from %s_tag where ktag='".$array[$key]."'")){
						$king->db->insert('%s_tag',$_array[$key]);	
						if($id=='1'){
							$king->db->query("delete from %s_key where ktitle like '%".$val."%'");	
						}
			}	
		}

		
	}


public function k_xggjc($array){
		global $king;	
		if(!empty($array)){
		$array=explode(',',$array);

		foreach($array as $key => $val){

			if($data=$king->db->getRows_one("select kid,ktitle from %s_key where ktitle='".$val."'")){	
	
				$s.= '<a href="/newk/newtag/'.$data['kid'].'/">'.$data['ktitle'].'</a>';				
			}
		}

		}
		return $s;

}


public function k_dangdang_fc($d,$a='3'){

$so = scws_new();
$so->set_charset('utf8');
// 这里没有调用 set_dict 和 set_rule 系统会自动试调用 ini 中指定路径下的词典和规则文件
$so->send_text($d);


//这里是提取权重最高的前20名，还有其他函数要用去看手册了
$tmp = $so->get_tops($a);


for($i=0; $i<$a; $i++){
	if(!empty($tmp[$i]['word'])){
    $str .=$tmp[$i]['word'].",";
	}
}
//$str = substr($str, 0, -1);
$str = mb_substr($str, 0, -1, "utf8");

$so->close();


return $str;

}



public function length($str){

             $len=strlen($str);

                 $i=0;

     while($i<$len){

     if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str[$i])){

                     $i+=2;

                         }else{

                             $i+=1;

                         }

                             $n+=1;

                         }

                     Return $n;

     }





}

?>
